package view;

import bean.Customer;
import service.CustomerList;
import util.CMUtility;

/**
 * @description:
 * @param: null
 * @return: 
 * @author Admin
 * @date: 2021/3/27 15:39
 */ 
public class CustomerView {
CustomerList customerList=new CustomerList(10);

    public CustomerView() {
        Customer customer=new Customer("lei",'男',23,"15279221770","7067@qq.com");
        customerList.addCustomer(customer);
    }

    public void enterMainMenu(){
        boolean loopFlag=true;
        do {
        System.out.println("-----------------客户信息管理软件-----------------\n" +
                "\n" +
                "                                     1 添 加 客 户\n" +
                "                                     2 修 改 客 户\n" +
                "                                     3 删 除 客 户\n" +
                "                                     4 客 户 列 表\n" +
                "                                     5 退           出\n" +
                "\n" +
                "                                     请选择(1-5)：");
        int index= CMUtility.readInt();
        switch(index){
            case 1:addNewCustomer();
            break;
            case 2:modifyCustomer();
            break;
            case 3:deleteCustomer();
            break;
            case 4:listAllCustomers();
            break;
            case 5:
                System.out.println("确认是否退出(Y/N)：");
                char c=CMUtility.readConfirmSelection();
                if (c=='y'){
                    return;
                }else break;
        }
    }while (loopFlag);

    }
    private void addNewCustomer(){
        System.out.println("---------------------添加客户---------------------\n");
        System.out.println("姓名：");
        String name=CMUtility.readString(4);
        System.out.println("性别：");
        char gender=CMUtility.readChar();
        System.out.println("年龄：");
        int age=CMUtility.readInt();
        System.out.println("电话：");
        String phone=CMUtility.readString(11);
        System.out.println("邮箱：");
        String email=CMUtility.readString(15);
        Customer cust=new Customer(name,gender,age,phone,email);
        boolean flag=customerList.addCustomer(cust);
        if (flag){
            System.out.println("---------------------添加完成---------------------");
        }else {
            System.out.println("---------------------添加数量达到上限，添加失败---------------------");
        }
    }
    private void modifyCustomer(){
        System.out.println("---------------------修改客户---------------------\n");
        int index=0;
        Customer cust=null;
        for (;;){
        System.out.println("请选择待修改客户编号(-1退出)：");
         index=CMUtility.readInt();
        if (index==-1){
            return;
        }else
             cust=customerList.getCustomer(index-1);
        if (cust==null){
            System.out.println("---------------------没有找到客户信息---------------------");
            }
        else break;

        }
            System.out.println("姓名("+cust.getName()+")：<直接回车表示不修改>");
            String name=CMUtility.readString(4,cust.getName());
            System.out.println("性别("+cust.getGender()+")：");
            char gender=CMUtility.readChar();
            System.out.println("年龄("+cust.getAge()+")：");
            int age=CMUtility.readInt();
            System.out.println("电话("+cust.getPhone()+")：");
            String phone=CMUtility.readString(11,cust.getPhone());
            System.out.println("邮箱("+cust.getEmail()+")：");
            String email=CMUtility.readString(15,cust.getEmail());
            cust=new Customer(name,gender,age,phone,email);
            boolean flag=customerList.replaceCustomer(index-1,cust);
            if (flag){
                System.out.println("添加成功");
            }else {
                System.out.println("----------无法找到指定客户,修改失败--------------");
            }

    }
    private void deleteCustomer(){
        int index=0;
        Customer customer=null;
        System.out.println("---------------------删除客户---------------------");
        for (;;) {
            System.out.println("请选择待删除客户编号(-1退出)：");
            index = CMUtility.readInt();
            if (index==-1){
               return;
            }customer=customerList.getCustomer(index-1);
            if (customer==null){
                System.out.println("无法找到指定客户！");
            }else break;
        }
        System.out.println("确认是否删除(Y/N)：");
        char c=CMUtility.readConfirmSelection();
        if (c=='y')return;
        boolean flag=customerList.deleteCustomer(index-1);
        if (flag){
            System.out.println("成功删除");
        }else System.out.println("----------无法找到指定客户,删除失败--------------");



    }
    private void listAllCustomers(){
        System.out.println("---------------------------客户列表---------------------------\n");
        Customer[]custs=customerList.getAllCustomers();
        if (custs.length==0){
            System.out.println("没有客户记录！");

        }else {
            System.out.println("编号\t姓名\t\t性别\t年龄\t\t电话\t\t邮箱");
            for (int i=0;i<custs.length;i++){
                System.out.println(i + 1 + "\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail());
            }
        }
        System.out.println("-------------------------客户列表完成-------------------------");
    }
    public static void main(String[] args){
        CustomerView cView = new CustomerView();
        cView.enterMainMenu();


    }

}
